#include <iostream>
#include <fstream>
#include <google/protobuf/unknown_field_set.h>
#include "contacts.pb.h"

void PrintContact(s_contacts::Contacts &contact)
{
	for (int i = 0; i < contact.contacts_size(); i++)
	{
		s_contacts::PeopleInfo people = contact.contacts(i);
		std::cout << "-----------联系人(" << i + 1 << ")-----------" << std::endl;
		std::cout << "姓名：" << people.name() << std::endl;
		std::cout << "年龄：" << people.age() << std::endl;
		int j = 1;
		for (const auto phone : people.phone())
		{
			std::cout << "电话(" << j++ << "): " << phone.number() << std::endl;
		}

		const google::protobuf::Reflection *reflection = s_contacts::PeopleInfo::GetReflection();
		const google::protobuf::UnknownFieldSet &unknowset = reflection->GetUnknownFields(people);
		for (int j = 0; j < unknowset.field_count(); j++)
		{
			const google::protobuf::UnknownField &field = unknowset.field(j);
			std::cout << "未知字段" << j + 1 << ":" << " 字段编号: " << field.number() << " 类型: " << field.type();
			switch(field.type())
			{
			case google::protobuf::UnknownField::Type::TYPE_VARINT:
				std::cout << " 值: " << field.varint() << std::endl;
				break;
			case google::protobuf::UnknownField::Type::TYPE_LENGTH_DELIMITED:
				std::cout << " 值: " << field.length_delimited() << std::endl;
				break;
			}
		}
	}
}

int main()
{
	s_contacts::Contacts contact;
	// 从本地文件中读取联系人
	std::fstream input("../contach.bin", std::ios::in | std::ios::binary);
	if (!input.is_open())
	{
		std::cout << "open error" << std::endl;
	}
	else
	{
		// 打开文件成功进行反序列化
		contact.ParseFromIstream(&input);
	}

	// 打印联系人列表
	PrintContact(contact);
	input.close();
	// std::cout << "succeed---" << std::endl;
	return 0;
}